<?php
header("Content-Type: text/html; charset=iso-8859-1");
require("include.php");
if(!check_admin()){
	redirect('perso.php?action=admin');
}
else{
	$_SESSION['navigation'] = 'compta_admin';
	################# Some variables####################
	$coti_junior = '250';
	$coti_mini = '160';
	$coti_senior = '350';
	$licence_regional = '70';
	$licence_junior = '40';
	$licence_national = '140';
	$licence_mini = '15';
	#####################################################

	$total_tarif_licence = 0;
	$total_tarif_cotisation = 0;
	$action = (!empty($_REQUEST['action'])) ? $_REQUEST['action'] : "";
	$filtre = (!empty($_REQUEST['filtre'])) ? $_REQUEST['filtre'] : "";
	if($action == 'update'){
		$membre = (!empty($_REQUEST['membre'])) ? $_REQUEST['membre'] : "";
		$field = (!empty($_REQUEST['field'])) ? $_REQUEST['field'] : "";
		$value = (!empty($_REQUEST['value'])) ? $_REQUEST['value'] : "";
		if($membre && $field){
			$q = "update comptabilite set $field = '$value' where membre_id = '$membre'";
			if(ExecRequete($q)){
				echo 1;	
				$mytable = unserialize($_SESSION['tablecompta']);
				foreach($mytable->rows as $row_id => $row){
					foreach($row->cols as $col){
						if(stripos($col,"update_compta('$membre','$field'") !== false){
							if($field == 'cotisation' || $field == 'licence' || $field == 'commande_licence'){
								if($value == 1) $mytable->rows[$row_id]->cols[$field] = str_replace("'checkbox'","'checkbox' checked = 'checked'",$col);
								else $mytable->rows[$row_id]->cols[$field] = str_replace("checked = 'checked'","",$col);
							}
							elseif($field == 'tarif_cotisation' || $field == 'tarif_licence'){
								$mytable->rows[$row_id]->cols[$field] = preg_replace("/value.=.\'[^\']*\'/","value = '$value'",$col);								
							}
							elseif($field == 'date_payement'){
								$date_parts = explode("-",$value);
								$mytable->rows[$row_id]->cols[$field] = preg_replace("/_jour' value.=.\'[^\']*\'/","_jour' value = '$date_parts[2]'",$col);				
								$mytable->rows[$row_id]->cols[$field] = preg_replace("/_mois' value.=.\'[^\']*\'/","_mois' value = '$date_parts[1]'",$mytable->rows[$row_id]->cols[$field]);				
								$mytable->rows[$row_id]->cols[$field] = preg_replace("/_annee' value.=.\'[^\']*\'/","_annee' value = '$date_parts[0]'",$mytable->rows[$row_id]->cols[$field]);				

							}
						}
					}
				}
				$_SESSION['tablecompta'] = serialize($mytable);
			} 
			else echo 0;
		}
		else echo 0;
	}
	elseif($action=='print'){
		$tab_equipes = array();
		$q = "select id,id_vbcll from equipes";
		$r = ExecRequete($q);
		while($s = LigneSuivante($r)){
			$tab_equipes[$s->id] = $s->id_vbcll;
		}
		$q = "select distinct(membres.id) as membre from membres inner join membre_equipes on membres.id = membre_id where membres.valide = 1";
		$r = ExecRequete($q);
		$nb_row = mysql_num_rows($r);
		$table = new table("compta");
		$fields = array("Pr&eacute;nom" => "prenom",
						"Nom" => "nom", 
						"Equipes" => "equipes", 
						"Coti. pay&eacute;" => "cotisation",
						"Lic. pay&eacute;" => "licence",
						"Comm." => "commande_licence",
						"Tarif cotis." => "tarif_cotisation",
						"Tarif licence" => "tarif_licence",
						"Date" => "date_payement");
		foreach($fields as $print => $field){
				$$field = new table_field($field,"compta");
				$$field->print = $print;
				if($field != "prenom" && $field != 'nom' && $field != 'equipes' )$$field->styleclass = 'center';
				if($field == 'date_payement') $date_payement->styleclass = 'nowrap';
				$table->fields[] = $$field;
		}
		$valide->filter = 0;	
		$table->order =  "nom";
		$table->filter = 1;
		$table->nb_rows = $nb_row;
		$table->onclick = "";
		$fields_compta = describe('comptabilite');
		while($s = LigneSuivante($r)){
			$membre = new membre($s->membre);
			if(($filtre == 'non-paye' && (!$membre->comptabilite["cotisation"] || !$membre->comptabilite['licence'])) || ($filtre == 'paye' && $membre->comptabilite['cotisation'] && $membre->comptabilite['licence']) || $filtre == 'tous'){
				$row = new table_row();
				if(!count($membre->equipes)) $row->hidden = 1;
				foreach($fields as $print => $field){
					if($field == 'prenom' || $field == 'nom') $print_field = $membre->$field;
					elseif($field == 'equipes') $print_field = implode(", ",$membre->equipes);
					else{
						$print_field = "";
						foreach($fields_compta as $key => $type){
							if($key == $field){
								if($type == 'int(1)'){
									$checked = ($membre->comptabilite[$field]) ? "checked = 'checked'" : "";
									$print_field = "<input type = 'checkbox' $checked value = '1' onclick = \"var value = (this.checked) ? 1 : 0; update_compta('$membre->id','$field',value);update_compta('$membre->id','tarif_$field',document.getElementById('".$membre->id."_tarif_$field').value);\"/>";
								}
								elseif(strpos($type,"varchar") !== FALSE){
									$value = $membre->comptabilite[$field];
									${"total_".$field} += $value;
									if(!$value){
										$les_equipes = "";
										foreach($membre->equipes as $id_eq => $nom_eq){
											$les_equipes .= " ".$tab_equipes[$id_eq];
										}
										if($field == 'tarif_licence'){
											if(strpos($les_equipes,"C") !== FALSE || strpos($les_equipes,"D") !== FALSE) $value = $licence_mini;
											if(strpos($les_equipes,"JA") !== FALSE || strpos($les_equipes,"JB") !== FALSE) $value = $licence_junior;
											if(strpos($les_equipes,"F1") !== FALSE || strpos($les_equipes,"F2") !== FALSE || strpos($les_equipes,"M3") !== FALSE || strpos($les_equipes,"M4") !== FALSE)  $value = $licence_regional;
											if(strpos($les_equipes,"M1") !== FALSE || strpos($les_equipes,"M2") !== FALSE) $value = $licence_national;
										}
										elseif($field == 'tarif_cotisation'){
											if(strpos($les_equipes,"M1") !== FALSE) $value = 0;
											elseif(strpos($les_equipes,"C") !== FALSE || strpos($les_equipes,"D") !== FALSE) $value = $coti_mini;

											elseif(strpos($les_equipes,"J") !== FALSE) $value = $coti_junior;
											else $value = $coti_senior;
										}
									}
									$print_field = "<input type = 'text' name = '$field' value = '$value' onblur = \"update_compta('$membre->id','$field',this.value)\"  size = '3' maxlength = '3' id = '".$membre->id."_$field' />";
								}
								elseif($type == 'date'){
									if((ereg("([0-9]{4}).([0-9]{2}).([0-9]{2})",$membre->comptabilite[$field],$regs))&&($membre->comptabilite['licence']||$membre->comptabilite['cotisation'])){
										$jour = $regs[3];
										$mois = $regs[2];
										$annee = $regs[1];
									}
									else{
										$jour = "";
										$mois = "";
										$annee = "";
									}
									$print_field = "<span style = 'visibility: hidden; display: none'>$annee.$mois.$jour</span><input type = 'text' size = '2' maxlength = '2' id = '".$membre->id."_jour' value = '$jour' onblur = \"if(document.getElementById('".$membre->id."_jour').value && document.getElementById('".$membre->id."_mois').value && document.getElementById('".$membre->id."_annee').value) update_compta('$membre->id','$field',document.getElementById('".$membre->id."_annee').value+'-'+document.getElementById('".$membre->id."_mois').value+'-'+document.getElementById('".$membre->id."_jour').value)\"/>";
										$print_field .= "<input type = 'text' size = '2'  maxlength = '2' id = '".$membre->id."_mois' value = '$mois' onblur = \"if(document.getElementById('".$membre->id."_jour').value && document.getElementById('".$membre->id."_mois').value && document.getElementById('".$membre->id."_annee').value) update_compta('$membre->id','$field',document.getElementById('".$membre->id."_annee').value+'-'+document.getElementById('".$membre->id."_mois').value+'-'+document.getElementById('".$membre->id."_jour').value)\"/>";
										$print_field .= "<input type = 'text' size = '4'  maxlength = '4'  id = '".$membre->id."_annee' value = '$annee' onblur = \"if(document.getElementById('".$membre->id."_annee').value && document.getElementById('".$membre->id."_mois').value && document.getElementById('".$membre->id."_annee').value) update_compta('$membre->id','$field',document.getElementById('".$membre->id."_annee').value+'-'+document.getElementById('".$membre->id."_mois').value+'-'+document.getElementById('".$membre->id."_jour').value)\"/>";
								}
							}
						}
					}
					$row->cols[$field] = $print_field;
				}
				$table->rows[] = $row;			
			}
		}
		echo $table->print_header();
		echo $table->print_body();
		echo $table->print_footer();	
		if($filtre == 'paye'){
			if(ereg("([0-9]+)([0-9]{3})",$total_tarif_cotisation,$regs)) $print_cotisation = $regs[1]."'".$regs[2];
			else $print_cotisation= $total_tarif_cotisation;
			if(ereg("([0-9]+)([0-9]{3})",$total_tarif_licence,$regs)) $print_licence = $regs[1]."'".$regs[2];
			else $print_licence= $total_tarif_licence;

			echo "<p class = 'center bold'>Total cotisations : $print_cotisation.-&nbsp;&nbsp;Total licences : $print_licence.-</p>\n";
		}
	}


	else{
		entete('Comptabilit&eacute;',"","",1);
		echo "<h1>Licences et cotisations</h1>\n";
		echo "<form>\n";
		echo "<p class = 'center'><select onChange=\"if(this.selectedIndex!=0) print_compta(this.options[this.selectedIndex].value)\">\n";
		echo "<option value = ''>Choisir une option d'affichage...</option>\n";
		echo "<option value=\"non-paye\">N'afficher que les entr&eacute;es non pay&eacute;es\n";
		echo "<option value=\"tous\">Afficher les entr&eacute;es pay&eacute;es et non pay&eacute;es\n";
		echo "<option value=\"paye\">N'afficher que les entr&eacute;es pay&eacute;es\n";
		echo "</select></p>\n";
		echo "</form> \n";
		echo "<div id = 'action'>\n";
		echo "</div>\n";
		echo "<script type = 'text/javascript'>print_compta('non-paye')</script>\n";
		pied();	
	}	
}

?>
